<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="T1Dbmn0YmNZwwxNqq98b7FvVCsxbeIChYP6H75TZjdk">
  <meta name="baidu-site-verification" content="code-86L5xW0tJy">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.whatsblockchain.com","root":"/","scheme":"Gemini","version":"8.0.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="跨链交互根据所跨越的区块链底层技术平台的不同可以分为同构链跨链和异构链跨链：同构链之间安全机制、共识算法、网络拓扑、区块生成验证逻辑都一致，它们之间的跨链交互相对简单。而异构链的跨链交互相对复杂，比如比特币采用PoW算法而Tendermint采用PBFT共识算法，其区块的组成形式和确定性保证机制均有很大不同，直接跨链交互机制不易设计。异构链之间的跨链交互一般需要第三方辅助服务辅助跨链交互。">
<meta property="og:type" content="article">
<meta property="og:title" content="Cosmos跨链协议">
<meta property="og:url" content="https://www.whatsblockchain.com/posts/ce363f86.html">
<meta property="og:site_name" content="区块链大杂烩">
<meta property="og:description" content="跨链交互根据所跨越的区块链底层技术平台的不同可以分为同构链跨链和异构链跨链：同构链之间安全机制、共识算法、网络拓扑、区块生成验证逻辑都一致，它们之间的跨链交互相对简单。而异构链的跨链交互相对复杂，比如比特币采用PoW算法而Tendermint采用PBFT共识算法，其区块的组成形式和确定性保证机制均有很大不同，直接跨链交互机制不易设计。异构链之间的跨链交互一般需要第三方辅助服务辅助跨链交互。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.whatsblockchain.com/assets/2019-09-13/ibc_process.svg">
<meta property="og:image" content="https://www.whatsblockchain.com/assets/2019-09-13/plugin.jpg">
<meta property="og:image" content="https://www.whatsblockchain.com/assets/2019-09-13/cross_chain.jpg">
<meta property="og:image" content="https://www.whatsblockchain.com/assets/2019-09-13/hub.png">
<meta property="article:published_time" content="2019-09-13T04:00:00.000Z">
<meta property="article:modified_time" content="2020-09-16T06:54:15.220Z">
<meta property="article:author" content="吴寿鹤">
<meta property="article:tag" content="区块链共识算法">
<meta property="article:tag" content="Tendermint">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.whatsblockchain.com/assets/2019-09-13/ibc_process.svg">


<link rel="canonical" href="https://www.whatsblockchain.com/posts/ce363f86.html">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Cosmos跨链协议 | 区块链大杂烩</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-145440251-1"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-145440251-1');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9af0452214522472f44968f5a652e2f0";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">区块链大杂烩</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">你想要的这里都有</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%8C%E6%9E%84%E8%B7%A8%E9%93%BE%E6%80%8E%E4%B9%88%E5%AE%9E%E7%8E%B0%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">同构跨链怎么实现？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%8C%E6%9E%84%E8%B7%A8%E9%93%BE%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95"><span class="nav-number">2.</span> <span class="nav-text">同构跨链实现方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B7%A8%E9%93%BE%E5%8D%8F%E8%AE%AE"><span class="nav-number">2.1.</span> <span class="nav-text">跨链协议</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#IBCRegisterChainTx"><span class="nav-number">2.1.1.</span> <span class="nav-text">IBCRegisterChainTx</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#IBCUpdateChainTx"><span class="nav-number">2.1.2.</span> <span class="nav-text">IBCUpdateChainTx</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#IBCPacketCreateTx"><span class="nav-number">2.1.3.</span> <span class="nav-text">IBCPacketCreateTx</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#IBCPacketPostTx"><span class="nav-number">2.1.4.</span> <span class="nav-text">IBCPacketPostTx</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#plugin"><span class="nav-number">2.2.</span> <span class="nav-text">plugin</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E6%9E%84%E8%B7%A8%E9%93%BEPegZone"><span class="nav-number">3.</span> <span class="nav-text">异构跨链PegZone</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#cosmos-hub%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">4.</span> <span class="nav-text">cosmos hub的作用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#hub%E5%B7%A5%E4%BD%9C%E6%96%B9%E5%BC%8F"><span class="nav-number">4.1.</span> <span class="nav-text">hub工作方式</span></a></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">吴寿鹤</p>
  <div class="site-description" itemprop="description">代码之美，在于架构，架构之美，在于抽象</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">21</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </section>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.whatsblockchain.com/posts/ce363f86.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="吴寿鹤">
      <meta itemprop="description" content="代码之美，在于架构，架构之美，在于抽象">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="区块链大杂烩">
    </span>

    
    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Cosmos跨链协议
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-09-13 12:00:00" itemprop="dateCreated datePublished" datetime="2019-09-13T12:00:00+08:00">2019-09-13</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-09-16 14:54:15" itemprop="dateModified" datetime="2020-09-16T14:54:15+08:00">2020-09-16</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Tendermint/" itemprop="url" rel="index"><span itemprop="name">Tendermint</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
      </div>
      <div class="post-meta">
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>4 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>跨链交互根据所跨越的区块链底层技术平台的不同可以分为同构链跨链和异构链跨链：同构链之间安全机制、共识算法、网络拓扑、区块生成验证逻辑都一致，它们之间的跨链交互相对简单。而异构链的跨链交互相对复杂，比如比特币采用PoW算法而Tendermint采用PBFT共识算法，其区块的组成形式和确定性保证机制均有很大不同，直接跨链交互机制不易设计。异构链之间的跨链交互一般需要第三方辅助服务辅助跨链交互。</p>
<a id="more"></a>
<h2 id="同构跨链怎么实现？"><a href="#同构跨链怎么实现？" class="headerlink" title="同构跨链怎么实现？"></a>同构跨链怎么实现？</h2><p>基于Tendermint开发的链都可以用同构跨链，cosmos中同构链之间进行资产转移的原理如下。</p>
<p>由于Tendermint采用PBFT+POS共识算法，每个区块都是经过全网2/3节点签名过的，所以得到Validator信息就可以通过对区块头进行校验来验证这个区块头是不是某一个链中的合法区块。ChainA，ChainB都是用Tendermint开发的链，现在ChainA，ChainB之间需要进行跨链资产转移，A,B两个链之间会相互进行注册，在注册的过程中，A，B两个链会将各自的创世区块以及ChainID（用于表示不同链）发送给对方，由于创世区块中含有Validator信息，所以通过注册后A，B两个链会有对方链的Validator信息，以及区块头信息。</p>
<p>现在A中的资产需要向B中转移，首先发送一个跨链交易package到A中，A执行这个packageTx，将相关的资产进行销毁或锁定，接下来将packageTx写入egress中，，egress可以看成是一个信箱，所有向外部通知的跨链交易的都放入到egress中。</p>
<p>为了将A链中发生的事件通知给B链，需要一个relayer，relayer是一个中继程序负责将A链中egress中的跨链消息转发给B链，relayer查询 A 链中 egress 中的packageTx，并获取packageTx的Merkle Proof，将这些信息打包成 IBCPacketPostTx 交易发送到 B 链上，并查询 packageTx 所在的区块头信息，将区块头信息打包成 IBCUpdateChainTx 发送到 B 链上， 注意这里 relayer是需要支付 IBCPacketPostTx，IBCUpdateChainTx 执行的交易费用的。</p>
<p>B链接收到IBCPacketPostTx交易后开始执行，首先通过A链中的Validator校验IBCUpdateChainTx中的区块头是否是A链的，然后校验IBCPacketPostTx中跨链交易的Merkle proof是否等于IBCUpdateChainTx中的区块头hash。当所有校验全部通过时B链开始执行相关操作。</p>
<p><img src="/assets/2019-09-13/ibc_process.svg" alt="ibc_process"></p>
<h2 id="同构跨链实现方法"><a href="#同构跨链实现方法" class="headerlink" title="同构跨链实现方法"></a>同构跨链实现方法</h2><h3 id="跨链协议"><a href="#跨链协议" class="headerlink" title="跨链协议"></a>跨链协议</h3><p>cosmos中的跨链是通过IBC协议实现，cosmos中一共定义以下几个IBC协议包:IBCRegisterChainTx,IBCUpdateChainTx,IBCPacketCreateTx,IBCPacketPostTx。</p>
<h4 id="IBCRegisterChainTx"><a href="#IBCRegisterChainTx" class="headerlink" title="IBCRegisterChainTx"></a>IBCRegisterChainTx</h4><p>在跨链开始用来注册，并发送创世区块，Validator给对方，这个只能执行一次，多次执行会报错。</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> IBCRegisterChainTx <span class="keyword">struct</span> &#123;</span><br><span class="line">	BlockchainGenesis</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> BlockchainGenesis <span class="keyword">struct</span> &#123;</span><br><span class="line">	ChainID <span class="keyword">string</span></span><br><span class="line">	Genesis <span class="keyword">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="IBCUpdateChainTx"><a href="#IBCUpdateChainTx" class="headerlink" title="IBCUpdateChainTx"></a>IBCUpdateChainTx</h4><p>向另外一个链推送当前链上区块最新信息，区块高度，区块头信息。</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> IBCUpdateChainTx <span class="keyword">struct</span> &#123;</span><br><span class="line">	Header tm.Header</span><br><span class="line">	Commit tm.Commit</span><br><span class="line">	<span class="comment">// <span class="doctag">TODO:</span> NextValidators</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="IBCPacketCreateTx"><a href="#IBCPacketCreateTx" class="headerlink" title="IBCPacketCreateTx"></a>IBCPacketCreateTx</h4><p>当链收到这个交易包时会执行跨链交易，并将在egress中放入相关信息。</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> IBCPacketCreateTx <span class="keyword">struct</span> &#123;</span><br><span class="line">	Packet</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> Packet <span class="keyword">struct</span> &#123;</span><br><span class="line">	SrcChainID <span class="keyword">string</span></span><br><span class="line">	DstChainID <span class="keyword">string</span></span><br><span class="line">	Sequence   <span class="keyword">uint64</span></span><br><span class="line">	Type       <span class="keyword">string</span> <span class="comment">// redundant now that Type() is a method on Payload ?</span></span><br><span class="line">	Payload    Payload</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="IBCPacketPostTx"><a href="#IBCPacketPostTx" class="headerlink" title="IBCPacketPostTx"></a>IBCPacketPostTx</h4><p>这个包中包含跨链交易执行后的Merkle proof，由relayer发送给另外一个链。</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> IBCPacketPostTx <span class="keyword">struct</span> &#123;</span><br><span class="line">	FromChainID     <span class="keyword">string</span> <span class="comment">// The immediate source of the packet, not always Packet.SrcChainID</span></span><br><span class="line">	FromChainHeight <span class="keyword">uint64</span> <span class="comment">// The block height in which Packet was committed, to check Proof</span></span><br><span class="line">	Packet</span><br><span class="line">	Proof *merkle.IAVLProof <span class="comment">// Merkle证明</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="plugin"><a href="#plugin" class="headerlink" title="plugin"></a>plugin</h3><p>那么如何执行这些跨链协议，从上面协议的定义可以看出，这些协议包其实都是一个交易，Tendermint为了方便我们扩展，Tendermint中有一个插件模块，我们可以实现plugin中接口，在ibc插件中执行跨链交易。</p>
<p><img src="/assets/2019-09-13/plugin.jpg" alt="plugin"></p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Plugin <span class="keyword">interface</span> &#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Name of this plugin, should be short.</span></span><br><span class="line">	Name() <span class="keyword">string</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">// Run a transaction from ABCI DeliverTx</span></span><br><span class="line">	RunTx(store KVStore, ctx CallContext, txBytes []<span class="keyword">byte</span>) (res abci.Result)</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Other ABCI message handlers</span></span><br><span class="line">	SetOption(store KVStore, key, value <span class="keyword">string</span>) (log <span class="keyword">string</span>)</span><br><span class="line">	InitChain(store KVStore, vals []*abci.Validator)</span><br><span class="line">	BeginBlock(store KVStore, hash []<span class="keyword">byte</span>, header *abci.Header)</span><br><span class="line">	EndBlock(store KVStore, height <span class="keyword">uint64</span>) abci.ResponseEndBlock</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面的代码时plugin接口的定义，可以看出plugin与abci接口很类似，所以在deliverTx时将ibc交易交给plugin</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ABCI::DeliverTx</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(app *BaseApp)</span> <span class="title">DeliverTx</span><span class="params">(txBytes []<span class="keyword">byte</span>)</span> <span class="params">(res abci.Result)</span></span> &#123;</span><br><span class="line">  <span class="comment">// Exec tx</span></span><br><span class="line">  <span class="keyword">switch</span> tx := tx.(<span class="keyword">type</span>) &#123;</span><br><span class="line">  <span class="keyword">case</span> *types.SendTx:</span><br><span class="line">      <span class="comment">// 执行正常交交易</span></span><br><span class="line">  <span class="keyword">case</span> *types.AppTx:</span><br><span class="line">    <span class="comment">// 执行plugin中的交易</span></span><br><span class="line">    plugin := pgz.GetByName(tx.Name)</span><br><span class="line">    res = plugin.RunTx(cache, ctx, tx.Data)</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line">  <span class="keyword">default</span>:</span><br><span class="line">    <span class="keyword">return</span> abci.ErrBaseEncodingError.SetLog(<span class="string">&quot;Unknown tx type&quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">	<span class="keyword">return</span> res</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="异构跨链PegZone"><a href="#异构跨链PegZone" class="headerlink" title="异构跨链PegZone"></a>异构跨链PegZone</h2><p>对于bitcoin,ethereum这类使用POW共识算法的链，如何与使用Tendermint的IBC协议进行跨链操作呢？由于这些链采用的POW算法那么我们无法通过Validator对这些链的区块头进行校验，进而无法使用Merkle proof来证明这些链上的跨链交易的合法行。其次POW共识算法产生的区块是属于概率最终性的，又被回滚的可能性，在进行跨链时我们需要保证跨链的交易是最终确定的且不会被回滚的。</p>
<p>基于以上几点考虑，我们采用PegZone方案来进行异构跨链，PegZone本身其实是一条用Tendermint开发的代理链，实时跟踪原始链的状态，通过设定一个安全阀值，等待原始链区块增长数达到安全阀值时就认为原始链的状态达到了伪实时最终性（回滚概率小），这点和轻客户端钱包验证是相同的原理，例如比特币安全阀值通常设置为6，以太坊安全阀值可以设置为20或者100。而PegZone本身具备实时最终性，就可以通过IBC与Hub相连，从而实现跨链。</p>
<p>接下来还是用例子来说明下，以Ethereum为例来看，有兴趣的朋友可参考Github上的<a target="_blank" rel="noopener" href="https://github.com/cosmos/peggy">cosmos/Peggy</a>项目。示意图如下：</p>
<p><img src="/assets/2019-09-13/cross_chain.jpg" alt="pegzone"></p>
<p>上图我们可以看出PegZone可以分为5个部分：</p>
<ul>
<li>Smart Contract：资产托管的角色，保管以太坊中的代币和Cosmos中的代币。主要提供了lock、unlock、mint、burn四个方法。</li>
<li>Witness：是一个以太坊全节点，监听以太坊合约的event，并等待100个区块产生后，封装WitnessTx提交到PegZone中来证明在以太坊内状态更改。</li>
<li>PegZone：PegZone是基于Tendermint的区块链，负责维护用户的账户信息，允许用户之间资产的转移，并提供交易查询。</li>
<li>Signer：使用secp256k1对交易进行签名，以便签名能够高效的被智能合约验证，对应于智能合约的校验者公钥集合。</li>
<li>Relayer： 中继器负责交易转发。将所有Signer签名后的SignTx转发到smart contract中。</li>
</ul>
<h2 id="cosmos-hub的作用"><a href="#cosmos-hub的作用" class="headerlink" title="cosmos hub的作用"></a>cosmos hub的作用</h2><p>在cosmos中自带的basecoin跨链demo中，两个链ChainA，ChainB，这两个链之间进行跨链，A，B两个链之间相互发送IBCRegisterChainTx进行注册。在跨链的时候直接发送IBC协议包，进行资产跨链操作。这种直接连接的方式会有个问题，随着接入到网络中Zone（zone是一个独立的区块链）的数量上升，以直连方式实现通信会导致链路数量呈平方级上升。以100个Zone接入到网络中为例，如果各个Zone直接都要建立起IBC连接，则网络中需要有n(n-1)/2=4950 条通信链路！如此快速的增长显然会令网络不堪重负。</p>
<p>通过hub就可以解决连接爆发增长的问题，所有的zone都向hub进行注册，向hub发送IBC包。</p>
<p><img src="/assets/2019-09-13/hub.png" alt="hub"></p>
<h3 id="hub工作方式"><a href="#hub工作方式" class="headerlink" title="hub工作方式"></a>hub工作方式</h3><p>hub管理着许多zone，所有的zone都需要向hub进行注册，由hub追踪各个zone的状态，每一个zone不停地把自身产出的新区快信息汇报给hub，同时每一个zone也需要同步hub的状态。每一个zone之间不是直接进行通信，而是通过向hub发送IBC间接进行通信。</p>
<p>Hub与Zone直接通信，而Zone与Zone之间通过IBC间接通信。当Zone对 Hub建立起一个IBC连接，它可以自动访问其他连接到该Hub上的Zone ，这意味着Zone无需与其他Zone连接，而仅仅连接到Hub上即可。</p>
<p>当一个Zone通过Hub收到来自其他Zone的代币时，它只需要信任Hub以及代币来源的Zone，而不需要信任网络中所有其它的Zone 。</p>

    </div>

    
    
    
      
  <div class="popular-posts-header">相关文章推荐</div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/e5f8fc7.html" rel="bookmark">深入Tendermint --- 出块节点选择</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/8ab37d7a.html" rel="bookmark">深入Tendermint---共识算法</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/ec5b8a8d.html" rel="bookmark">【分布式共识一】分布系统及共识介绍</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/ef92d986.html" rel="bookmark">【分布式共识三】拜占庭将军问题——书面协议</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="/posts/fe0496d6.html" rel="bookmark">【分布式共识二】拜占庭将军问题——口头协议</a></div>
    </li>
  </ul>


    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>原作者： </strong>吴寿鹤
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://www.whatsblockchain.com/posts/ce363f86.html" title="Cosmos跨链协议">https://www.whatsblockchain.com/posts/ce363f86.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E5%8C%BA%E5%9D%97%E9%93%BE%E5%85%B1%E8%AF%86%E7%AE%97%E6%B3%95/" rel="tag"># 区块链共识算法</a>
              <a href="/tags/Tendermint/" rel="tag"># Tendermint</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/2b442f1b.html" rel="prev" title="亲自动手实现一个公链">
                  <i class="fa fa-chevron-left"></i> 亲自动手实现一个公链
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/11d5a5c5.html" rel="next" title="十分钟开发零知识证明之混币--原理篇">
                  十分钟开发零知识证明之混币--原理篇 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
  
  
  



      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">吴寿鹤</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">87k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:19</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


















  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  
      <script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              const target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    const script = document.createElement('script');
    script.src = '//cdn.jsdelivr.net/npm/mathjax@3.1.0/es5/tex-mml-chtml.js';
    script.defer = true;
    document.head.appendChild(script);
  } else {
    MathJax.startup.document.state(0);
    MathJax.typesetClear();
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

</body>
</html>
